% To calculate percentage from counted transtion matrices
clear all;
clc;
load 97_09_L100k_final_Q.mat
freqj=4;% 4 quarters
year_list=[1997:2009]';
year_list_text=num2str(year_list);
quarter_list=[1:4]';
quarter_list_text=num2str(quarter_list);
Rate_list=[0,1:0.5:5];
prob=zeros(13,freqj,10,13);
prob_std=zeros(13,freqj,10,12);
default_prob=zeros(length(year_list)*freqj,1);
filename='prob_std_97_09_L100k_final_Q.xls';

% to calculate probability of each cell for first 12 lines.
for i=1:13
for j=1:freqj
    for ii=1:length(Rate_list)
        for jj=1:12
            prob(i,j,ii,jj) = final_result(i,j,ii,jj)/final_result(i,j,ii,13);            
        end
    end 
end 
end

% to calculate standadize probability using withdrawn probability
for i=1:13
    for j=1:freqj
        for ii=1:length(Rate_list)
            for jj=1:12
                prob_std(i,j,ii,jj) = prob(i,j,ii,jj)/(1-prob(i,j,ii,12));
            end
        end
        xx(:,:)=prob_std(i,j,:,:);
        sheetlist=strcat('',year_list_text(i,:),'.',quarter_list_text(j,:),'');
        xlswrite(filename, xx, sheetlist);
        clear xx sheetlist
    end
end
% to calculate default probability for each period (month or quarter)
ij=1;
for i=1:13
    for j=1:freqj
           default_prob(ij,1)=(sum(final_result(i,j,1:10,11))/sum(final_result(i,j,1:10,13)))/(1-(sum(final_result(i,j,1:10,12))/sum(final_result(i,j,1:10,13))));
           ij=ij+1;
     end
end
xx(:,:)=default_prob(:,1); 
xlswrite(filename, xx, 'default_prob');


